<?php
/**
 * Created by PhpStorm.
 * User: mayn
 * Date: 2018/8/31
 * Time: 13:10
 */

namespace app\api\validate;

use app\lib\exception\ParamerException;
use think\Exception;
use think\Request;
use think\Validate;

class BaseValidate extends Validate
{
    public function goCheck() {
        $params = Request::instance()->param();
        $result = $this->batch()->check($params);
        if (!$result) {
            throw new ParamerException([
                'msg' => $this->error,
                'error_code' => 10000
            ]);
        } else {
            return true;
        }
    }
    public function isPositiveInteger($value) {
        if (is_int($value + 0) && is_numeric($value) && $value + 0 > 0) {
            return true;
        } else {
            return false;
        }
    }
    public function getDataByRule($data) {
        $newData = [];
        $rule = $this->rule;
        // 直接终止程序，防止外部非法修改用户信息
        if (array_key_exists('user_id', $data) || array_key_exists('uid', $data)) {
            throw new ParamerException([
                'msg' => '参数中包含有非法的参数名user_id或者uid'
            ]);
        }
        foreach ($rule as $key => $value) {
            $newData[$key] = $data[$key];
        }
        return $newData;
    }

    public function isNotEmpty($value) {
        if (empty($value)) {
            return false;
        } else {
            return true;
        }
    }
}